import 'package:flutter/material.dart';

class SwitchCheckboxPage extends StatelessWidget {
  const SwitchCheckboxPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('选择单选开关和复选框组件')),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          const _SwitchAndCheckBoxTestRoute(),
          const _RadioDemoCard(),
          const _SwitchDemoCard(),
          const _CheckboxDemoCard(),
          const _CheckboxListTileDemoCard(),
        ],
      ),
    );
  }
}

class _SwitchAndCheckBoxTestRoute extends StatefulWidget {
  const _SwitchAndCheckBoxTestRoute();

  @override
  State<_SwitchAndCheckBoxTestRoute> createState() =>
      _SwitchAndCheckBoxTestRouteState();
}

class _SwitchAndCheckBoxTestRouteState
    extends State<_SwitchAndCheckBoxTestRoute> {
  bool _switchSelected = true; //维护单选开关状态
  bool _checkboxSelected = true; //维护复选框状态
  @override
  Widget build(BuildContext context) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          children: <Widget>[
            Switch(
              value: _switchSelected, //当前状态
              onChanged: (value) {
                //重新构建页面
                setState(() {
                  _switchSelected = value;
                });
              },
            ),
            Checkbox(
              value: _checkboxSelected,
              activeColor: Colors.red, //选中时的颜色
              onChanged: (value) {
                setState(() {
                  // 修复类型不匹配问题，使用空值合并运算符确保赋值为布尔值
                  _checkboxSelected = value ?? false;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}

class _RadioDemoCard extends StatefulWidget {
  const _RadioDemoCard();

  @override
  State<_RadioDemoCard> createState() => _RadioDemoCardState();
}

class _RadioDemoCardState extends State<_RadioDemoCard> {
  int? _selectedValue = 1;

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('单选按钮', style: Theme.of(context).textTheme.titleLarge),
            RadioListTile<int>(
              title: const Text('选项一'),
              value: 1,
              groupValue: _selectedValue,
              onChanged: (value) => setState(() => _selectedValue = value),
            ),
            RadioListTile<int>(
              title: const Text('选项二'),
              value: 2,
              groupValue: _selectedValue,
              onChanged: (value) => setState(() => _selectedValue = value),
            ),
          ],
        ),
      ),
    );
  }
}

class _SwitchDemoCard extends StatefulWidget {
  const _SwitchDemoCard();

  @override
  State<_SwitchDemoCard> createState() => _SwitchDemoCardState();
}

class _SwitchDemoCardState extends State<_SwitchDemoCard> {
  bool _switchValue = false;

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('开关组件', style: Theme.of(context).textTheme.titleLarge),
            SwitchListTile(
              title: const Text('启用功能'),
              value: _switchValue,
              onChanged: (value) => setState(() => _switchValue = value),
            ),
          ],
        ),
      ),
    );
  }
}

class _CheckboxDemoCard extends StatefulWidget {
  const _CheckboxDemoCard();

  @override
  State<_CheckboxDemoCard> createState() => _CheckboxDemoCardState();
}

class _CheckboxDemoCardState extends State<_CheckboxDemoCard> {
  bool _checked = false;
  bool _checked2 = true;

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('复选框', style: Theme.of(context).textTheme.titleLarge),
            CheckboxListTile(
              title: const Text('未选中状态'),
              value: _checked,
              onChanged: (value) => setState(() => _checked = value!),
            ),
            CheckboxListTile(
              title: const Text('已选中状态'),
              value: _checked2,
              onChanged: (value) => setState(() => _checked2 = value!),
            ),
          ],
        ),
      ),
    );
  }
}

class _CheckboxListTileDemoCard extends StatefulWidget {
  const _CheckboxListTileDemoCard();

  @override
  State<_CheckboxListTileDemoCard> createState() =>
      _CheckboxListTileDemoCardState();
}

class _CheckboxListTileDemoCardState extends State<_CheckboxListTileDemoCard> {
  bool _value1 = false;
  bool _value2 = false;

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('带标签的复选框', style: Theme.of(context).textTheme.titleLarge),
            CheckboxListTile(
              title: const Text('选项A'),
              subtitle: const Text('附带说明信息'),
              secondary: const Icon(Icons.check_circle_outline),
              value: _value1,
              onChanged: (value) => setState(() => _value1 = value!),
            ),
            CheckboxListTile(
              title: const Text('选项B'),
              subtitle: const Text('禁用状态演示'),
              secondary: const Icon(Icons.block),
              value: _value2,
              onChanged: null,
            ),
          ],
        ),
      ),
    );
  }
}
